/*****扫码枪模块*******/

var scanGunCtrl = {
	keyCode : "",
	curAct : 1, // 1:产品页面动作 2：开单页面
	gdsCallBack : null, //产品页面扫码枪结果返回。
	receiptCallBack : null, //开单页面扫码枪结果返回。
	rKeyCode : "",//添加产品的时候用，跳到添加产品页面。
	
	isGun : true, //这里是全局监听扫码枪，其他输入框回车时候回 
};

/*****监听扫码枪事件，******/
scanGunCtrl.addEvent = function()
{
	
	let that = this;
	let time = new Date().getTime();
	this.keyCode = "";
	window.document.onkeypress = function (event) 
	{
		if(!that.isGun)
		{
			that.isGun = true;
			return;
		}

		if(!that.gdsCallBack && !that.receiptCallBack) return;

		let key = String.fromCharCode(event.keyCode);
		let now = new Date().getTime();
		let subTime = now - time;
		if(that.keyCode == "" || subTime < 80)
		{
			if(event.keyCode == 13)
			{
				scanGunCtrl.action();
				setTimeout(intervalFun,100);
				return; 
			}
			that.keyCode = that.keyCode + key;
		}
		else
		{
			that.keyCode = "";
		}
		time = now;
	}

	function intervalFun()
	{
		that.keyCode = "";
	}
}

/*****扫码枪动作处理****/
scanGunCtrl.action = function()
{
	if(this.curAct == 1)
	{
		this.goodsSearch(); //产品搜索
	}
	else if(this.curAct == 2)
	{
		this.receipt(); //开单页面产品搜索或加购（仅有无关联报价可以加购）
	}
	this.keyCode = "";
}

/****产品搜索*****/
scanGunCtrl.goodsSearch = function()
{
	if(this.gdsCallBack)
		this.gdsCallBack.apply(null,[this.keyCode]);
}

/****开单加购*****/
scanGunCtrl.receipt = function()
{
	if(this.receiptCallBack)
		this.receiptCallBack.apply(null,[this.keyCode]);
}

export default scanGunCtrl;